﻿#if NETSTANDARD2_0 || NETCOREAPP3_0
using Longbow.Logging;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Options;
using System;

namespace Microsoft.Extensions.DependencyInjection
{
    /// <summary>
    /// 文本日志配置服务注入方法扩展类
    /// </summary>
    public static class FileLoggerFactoryExtensions
    {
        #region ILoggerBuilder Interface
        /// <summary>
        /// 添加文件日志功能到服务容器中
        /// </summary>
        /// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
        /// <param name="configure"></param>
        public static ILoggingBuilder AddFileLogger(this ILoggingBuilder builder, Action<FileLoggerOptions> configure = null)
        {
            builder.Services.AddSingleton<ILoggerProvider, FileLoggerProvider>();
            builder.Services.AddSingleton<IConfigureOptions<FileLoggerOptions>, LoggerProviderConfigureOptions<FileLoggerOptions, FileLoggerProvider>>();
            builder.Services.AddSingleton<IOptionsChangeTokenSource<FileLoggerOptions>, LoggerProviderOptionsChangeTokenSource<FileLoggerOptions, FileLoggerProvider>>();
            builder.Services.RegisterExceptionManager();
            builder.Services.Configure<FileLoggerOptions>(op =>
            {
                op.FileName = op.FileName.GetOSPlatformPath();
                configure?.Invoke(op);
            });
            return builder;
        }
        #endregion
    }
}
#endif
